package com.tarena.lbs.user.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.encoder.JwtEncoder;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.pojo.param.AdminLoginParam;
import com.tarena.lbs.user.pojo.param.UserLoginParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.vo.LoginVO;
import com.tarena.lbs.user.service.CaptchaService;
import com.tarena.lbs.user.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class LoginServiceImpl implements LoginService {

    @Autowired
    private AdminRepository adminRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private CaptchaService captchaService;

    @Autowired
    private JwtEncoder jwtEncoder;


    @Override
    public LoginVO doLogin(AdminLoginParam param) throws BusinessException {
        // 1. 校验验证码
//        if (!captchaService.verifyCaptcha(captchaKey, captcha)) {
//            throw new BusinessException("-2", "验证码错误");
//        }

        // 2. 校验账号密码
        String phone = param.getPhone();
        AdminPO po = adminRepository.getAdminByPhone(phone);
        Asserts.isTrue(po == null, new BusinessException("-2", "该手机号不存在"));
        //matchPassword(param.getPassword(), po.getAccountPassword());
        boolean equals = BCrypt.checkpw(param.getPassword(), po.getAccountPassword());
        Asserts.isTrue(!equals, new BusinessException("-2", "密码不相等"));
        //Asserts.isTrue(captchaService.validateCode(param.getCode())==false, new BusinessException("-2", "验证码错误"));
        if(captchaService.validateCode(param.getCode())==false){
            throw  new BusinessException("-2", "验证码错误");
        }
        // 3. 生成JWT
        String jwt = generateAdminJwt(po);
        return new LoginVO(jwt, po.getId(), po.getNickname());
    }

    public LoginVO userLogin(UserLoginParam param) throws BusinessException {
        // 1. 校验验证码
//        if (!captchaService.verifyCaptcha(captchaKey, captcha)) {
//            throw new BusinessException("-2", "验证码错误");
//        }

        // 2. 校验账号密码
        String phone = param.getPhone();
        UserPO po = userRepository.getByPhone(phone);
        Asserts.isTrue(po == null, new BusinessException("-2", "该手机号不存在小程序用户"));
        //matchPassword(param.getPassword(), po.getPassword());
        boolean equals = BCrypt.checkpw(param.getPassword(), po.getPassword());
        Asserts.isTrue(!equals, new BusinessException("-2", "密码不相等"));


        // 3. 生成JWT
        String jwt = generateUserJwt(po);
        return new LoginVO(jwt, po.getId(), po.getNickName());
    }

    private String generateAdminJwt(AdminPO po) throws BusinessException {
        UserPrinciple userPrinciple = new UserPrinciple();
        userPrinciple.setId(po.getId());
        userPrinciple.setNickname(po.getNickname());
        userPrinciple.setRole(po.getAccountType() == 0 ? Roles.ADMIN : Roles.SHOP);
        return jwtEncoder.generateToken(userPrinciple);
    }

    private String generateUserJwt(UserPO po) throws BusinessException {
        UserPrinciple userPrinciple = new UserPrinciple();
        userPrinciple.setId(po.getId());
        userPrinciple.setNickname(po.getNickName());
        userPrinciple.setRole(Roles.USER);
        return jwtEncoder.generateToken(userPrinciple);
    }

//    private void matchPassword(String password, String accountPassword) throws BusinessException {
//        boolean equals = StringUtils.equals(password, accountPassword);
//        Asserts.isTrue(!equals, new BusinessException("-2", "密码不相等"));
//    }
}